//
//  MyTool.m
//  shangcheng
//
//  Created by 张艳江 on 2019/5/29.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "MyTool.h"
#import "SCNavigationController.h"
#import "SCLoginController.h"
#import "SCTabBarController.h"

@implementation MyTool

//拼接的urlStr
+ (NSString *)splicingWithUrl:(NSString *)url{
    return [NSString stringWithFormat:@"%@%@",HTTP_URL,url];
}

//设置label的行高
+ (void)setRowHeightWithLabel:(UILabel *)label{
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:label.text attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
    label.attributedText = attribute;
}
//获取label的高度
+ (CGFloat)getString:(NSString *)string lineSpacing:(CGFloat)lineSpacing font:(UIFont*)font width:(CGFloat)width {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = lineSpacing;
    NSDictionary *dic = @{ NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle };
    CGSize size = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    return  ceilf(size.height);
}

#pragma mark - 登录
+ (void)loginWithVc:(UIViewController *)vc{
    [SVProgressHUD showImage:nil status:@"请先登录"];
    SCLoginController *loginVc = [[SCLoginController alloc]init];
    SCNavigationController *nav = [[SCNavigationController alloc]initWithRootViewController:loginVc];
    [vc presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 时间戳转化时间
+ (NSString *)getTimeFromTimesTamp:(NSString *)timeStr{
    
    NSTimeInterval interval    = [timeStr doubleValue];
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString       = [formatter stringFromDate:date];
    return dateString;
}

+ (CGFloat)boundingRectWithString:(NSString *)string withHeight:(CGFloat)height withFont:(CGFloat)font{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
                                     context:nil];
    return rect.size.width;
}
#pragma mark - 有取消按钮的警示框
+ (UIAlertController *)alertTitle:(NSString *)title mesasge:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle  confirmHandler:(void(^)(UIAlertAction *))confirmHandler cancleHandler:(void(^)(UIAlertAction *))cancleHandler viewController:(UIViewController *)vc{
    
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    
    UIAlertAction *confirmAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:confirmHandler];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:cancleHandler];
    
    [alertController addAction:confirmAction];
    [alertController addAction:cancleAction];
    
    [vc presentViewController:alertController animated:YES completion:nil];
    
    return alertController;
}
#pragma mark - 警示框
+ (void)showAlertView:(NSString *)title message:(NSString *)message;{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma mark - 退出登录
+ (void)logout{
    NSUserDefaults *uf = [NSUserDefaults standardUserDefaults];
    [uf removeObjectForKey:@"token"];
    [uf synchronize];
    SCTabBarController *tabBarVc = [[SCTabBarController alloc]init];
    ApplicationDelegate.window.rootViewController = tabBarVc;
    [ApplicationDelegate.window makeKeyAndVisible];
    [MyTool loginWithVc:tabBarVc];
}

#pragma mark - 去掉多余的0
+ (NSString *)removeFloatAllZeroByString:(NSString *)testNumber{
    
    NSString *testStr = [NSString stringWithFormat:@"%.2f", [testNumber doubleValue]];
    NSString * outNumber = [NSString stringWithFormat:@"%@",@(testStr.floatValue)];
    return outNumber;
}

#pragma mark - 修改Labl字体大小和颜色
+ (NSMutableAttributedString *)setCusstomeLable:(UILabel *)lable withTextFont:(CGFloat)textFont withRangeString:(NSString *)rangeString withColor:(UIColor *)textColor{
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:lable.text];
    //设置字体
    [attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:textFont] range:[lable.text rangeOfString:rangeString]];
    
    [attrString addAttribute:NSForegroundColorAttributeName
                       value:textColor
                       range:[lable.text rangeOfString:rangeString]];
    
    lable.attributedText = attrString;
    return attrString;
}


@end
